package com.zoe.app.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import cn.hutool.json.JSONUtil;
import com.zoe.app.AddressUtils;
import com.zoe.app.OperationUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author zbg
 * @date 2023/09/07
 * @description
 */
@RestController
public class ReqController {
    public static void main(String[] args) {
        String realAddressByIP = AddressUtils.getRealAddressByIP("60.168.211.132");
        System.out.println(realAddressByIP);
    }

    @GetMapping("/")
    public Object getInfo() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = requestAttributes.getRequest();
        String clientIP = ServletUtil.getClientIP(request);
        String realAddressByIP = AddressUtils.getRealAddressByIP(clientIP);


        UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));

        // {
        //   "engineVersion": "537.36",
        //   "osUtilName": "Windows",
        //   "agent": "{\"mobile\":false,\"browser\":{\"name\":\"MSEdge\",\"pattern\":\"Edge|Edg\"},\"version\":\"116.0.1938.69\",\"platform\":{\"name\":\"Windows\",\"pattern\":\"windows\"},\"os\":{\"name\":\"Windows 10 or Windows Server 2016\",\"pattern\":\"windows nt 10\\\\.0\"},\"osVersion\":\"10.0\",\"engine\":{\"name\":\"Webkit\",\"pattern\":\"webkit\"},\"engineVersion\":\"537.36\"}",
        //   "address": "安徽省 合肥市",
        //   "os": "Windows 10 or Windows Server 2016",
        //   "osVersion": "10.0",
        //   "engine": "Webkit",
        //   "ip": "60.168.211.132",
        //   "platForm": "Windows",
        //   "bro": "MSEdge",
        //   "broVersion": "116.0.1938.69"
        // }
        Map<String, String> resMap = MapUtil.newHashMap();
        resMap.put("address", realAddressByIP);
        resMap.put("agent", JSONUtil.toJsonStr(userAgent));
        resMap.put("ip", clientIP);
        resMap.put("bro", userAgent.getBrowser().getName());
        resMap.put("broVersion", userAgent.getVersion());
        resMap.put("engine", userAgent.getEngine().getName());
        resMap.put("engineVersion", userAgent.getEngineVersion());
        resMap.put("os", userAgent.getOs().getName());
        resMap.put("osVersion", userAgent.getOsVersion());
        resMap.put("platForm", userAgent.getPlatform().getName());
        resMap.put("osUtilName", OperationUtil.getOSName(userAgent.getOs().getName()));

        return resMap;
    }

    @GetMapping("map")
    public Object map(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        System.out.println(map);
        return map;
    }
}
